package com.cqc.customviews;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.cqc.customviews.views.ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.et1)
    ClearEditText et1;
    @BindView(R.id.et2)
    EditText et2;
    private Drawable drawable2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        Drawable[] drawables = et2.getCompoundDrawables();
        et2.setCompoundDrawables(drawables[0], drawables[1], null, drawables[3]);

        if (null == drawable2) {
            drawable2 = getResources().getDrawable(R.drawable.edittext_delete);
        }
        drawable2.setBounds(0, 0, drawable2.getIntrinsicWidth(), drawable2.getIntrinsicHeight());

        et2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Drawable[] drawables = et2.getCompoundDrawables();
                Drawable drawableRight = drawables[2];
                if (drawableRight == null) {
                    return false;
                }
                if (event.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                if (event.getX() > (et2.getWidth() - et2.getPaddingRight() - drawableRight.getIntrinsicWidth())) {
                    et2.setText("");
                }
                return false;
            }
        });

        et2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (et2.getText().length() > 0) {
                        setClearVisible(true);
                    } else {
                        setClearVisible(false);
                    }
                }
            }
        });

        et2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    setClearVisible(true);
                } else {
                    setClearVisible(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void setClearVisible(boolean visible) {
        Drawable drawableRight = visible ? drawable2 : null;
        Drawable[] drawables = et2.getCompoundDrawables();
        et2.setCompoundDrawables(drawables[0], drawables[1], drawableRight, drawables[3]);
    }
}
